"""
基础 CoderGraph 示例

这个示例展示了如何创建和使用 CoderGraph 来处理编程相关的任务。
"""

import asyncio
import datetime
import os

from inscode_agent.graph.nodes.mcp import MCPNodes

from inscode_agent.mcps import MCPClientManager

from inscode_agent.graph.coder import BaseGraph
from inscode_agent.models.graph import GraphRequest, AgentStepLog


async def create_mcp_graph() -> BaseGraph:
    """创建 CoderGraph 实例"""
    mcp_client = MCPClientManager("mcp.json")
    return BaseGraph(
        graph_name="mcp_graph",
        nodes=[MCPNodes(mcp_client_manager=mcp_client)],
        agent_template="agent_base",
        parse_tool_args=True,
        mcp_client=mcp_client,
        verbose=True
    )


async def run_coder_example():
    """运行 CoderGraph 示例"""
    # 创建图形
    # 首先从官网安装：https://desktopcommander.app/ mcp
    graph = await create_mcp_graph()

    # 创建请求
    request = GraphRequest(
        question=f"{datetime.datetime.now()}：罗列数据表结构",
        thread_id="coder_example_001",
        verbose=True,
        kwargs={
            "cwd_dir": os.getcwd()
        }
    )

    # 执行请求并处理响应
    async for message in graph.astream(request):
        if isinstance(message, AgentStepLog) and message.meta["finish"] == True:
            print(message.output)


if __name__ == "__main__":
    asyncio.run(run_coder_example())
